import { defineStore } from 'pinia';
import type { RestoredImage, ImageMetadata } from '../types/models';
import imageRestorationService from '../services/ImageRestorationService';

export const useRestorationStore = defineStore('restoration', {
  state: () => ({
    stitchedImageFile: null as File | null,
    metadata: null as ImageMetadata | null,
    restoredImages: [] as RestoredImage[],
    isRestoring: false,
    restorationProgress: 0,
    isDownloading: false,
    downloadProgress: 0,
    validationResult: null as { valid: boolean; message?: string } | null,
    // 添加质量控制选项
    jpegQuality: 0.95, // JPEG默认质量95%
    pngQuality: 1.0,   // PNG默认质量100%
    preserveFormat: true // 默认保留原始格式
  }),
  
  actions: {
    // 清空所有还原相关的状态
    clearAll() {
      this.stitchedImageFile = null;
      this.metadata = null;
      this.restoredImages = [];
      this.isRestoring = false;
      this.restorationProgress = 0;
      this.validationResult = null;
    },
    
    // 只清空还原结果，保留上传的图片和元数据
    clearRestoredImages() {
      this.restoredImages = [];
    },
    
    // 添加缺少的方法
    setUploadedStitchedImage(file: File) {
      this.stitchedImageFile = file;
    },
    
    setUploadedMetadata(metadata: ImageMetadata | null) {
      this.metadata = metadata;
    },
    
    setRestoring(value: boolean) {
      this.isRestoring = value;
    },
    
    setRestorationProgress(progress: number) {
      this.restorationProgress = progress;
    },
    
    setRestoredImages(images: RestoredImage[]) {
      this.restoredImages = images;
    },
    
    // 设置JPEG质量
    setJpegQuality(quality: number) {
      this.jpegQuality = Math.max(0.1, Math.min(1.0, quality));
    },
    
    // 设置PNG质量
    setPngQuality(quality: number) {
      this.pngQuality = Math.max(0.1, Math.min(1.0, quality));
    },
    
    // 设置是否保留原始格式
    setPreserveFormat(preserve: boolean) {
      this.preserveFormat = preserve;
    },
    
    async validateMetadata(image: File, metadata: ImageMetadata) {
      this.validationResult = await imageRestorationService.validateMetadata(image, metadata);
      return this.validationResult;
    },
    
    async restoreImages() {
      if (!this.stitchedImageFile || !this.metadata) {
        throw new Error('Stitched image or metadata is missing');
      }
      
      if (!this.validationResult?.valid) {
        throw new Error(this.validationResult?.message || 'Invalid metadata');
      }
      
      this.isRestoring = true;
      this.restorationProgress = 0;
      
      try {
        // 传递用户设置的质量参数
        const restoredImages = await imageRestorationService.restoreImages(
          this.stitchedImageFile,
          this.metadata,
          (progress) => {
            this.restorationProgress = progress;
          },
          {
            jpegQuality: this.jpegQuality,
            pngQuality: this.pngQuality,
            preserveFormat: this.preserveFormat
          }
        );
        
        this.restoredImages = restoredImages;
        return restoredImages;
      } finally {
        this.isRestoring = false;
        this.restorationProgress = 100;
      }
    },
    
    async downloadRestoredImage(image: RestoredImage) {
      this.isDownloading = true;
      
      try {
        // 传递用户设置的质量参数
        await imageRestorationService.downloadRestoredImage(image, {
          jpegQuality: this.jpegQuality,
          pngQuality: this.pngQuality,
          preserveFormat: this.preserveFormat
        });
      } finally {
        this.isDownloading = false;
      }
    },
    
    async downloadAsZip() {
      if (this.restoredImages.length === 0) {
        throw new Error('No restored images to download');
      }
      
      this.isDownloading = true;
      this.downloadProgress = 0;
      
      try {
        await imageRestorationService.downloadAsZip(
          this.restoredImages,
          (progress) => {
            this.downloadProgress = progress;
          },
          {
            jpegQuality: this.jpegQuality,
            pngQuality: this.pngQuality,
            preserveFormat: this.preserveFormat
          }
        );
      } finally {
        this.isDownloading = false;
        this.downloadProgress = 100;
      }
    }
  }
});